package ua.org.nuos.sdms.middle.entity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 29.03.12
 * Time: 20:48
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "subject")
@NamedQueries(value = {
        @NamedQuery(name = "findSubjectByName", query = "SELECT DISTINCT s FROM Subject s WHERE s.name = :name AND s.group.id = :group_id ORDER BY s.name, s.status"),
        @NamedQuery(name = "findSubjectByTeacher", query = "SELECT DISTINCT s FROM Teacher t, Subject s WHERE t.id = :teacher_id AND t MEMBER OF s.teachers AND s.group.id = :group_id ORDER BY s.name, s.status"),
        @NamedQuery(name = "findSubjectByGroup", query = "SELECT DISTINCT s FROM Subject s WHERE s.group.id = :group_id ORDER BY s.name, s.status")
})
public class Subject {

    public enum Status {ACTIVE, IN_ARCHIVE}

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

    @Column(name = "name")
    private String name;

    @Lob
    @Column(name = "about")
    private String about;

    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST})
    @JoinTable(name = "teacher_subjects",
            joinColumns = {@JoinColumn(name = "teacher_fk")},
            inverseJoinColumns = {@JoinColumn(name = "subject_fk")})
    private List<Teacher> teachers;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "group_id", nullable = false)
    private Group group;

    @NotNull
    @Column(name = "status", nullable = false)
    @Enumerated(value = EnumType.STRING)
    private Status status;

    public Subject() {
        teachers = new LinkedList<Teacher>();
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public List<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    public void addTeacher(Teacher teacher) {
        if (teachers != null) {
            teachers.add(teacher);
        }
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Subject subject = (Subject) o;

        if (id != subject.id) return false;
        if (about != null ? !about.equals(subject.about) : subject.about != null) return false;
        if (name != null ? !name.equals(subject.name) : subject.name != null) return false;
        if (teachers != null ? !teachers.equals(subject.teachers) : subject.teachers != null) return false;
        if (group != null ? !group.equals(subject.group) : subject.group != null) return false;
        if (status != null ? !status.equals(subject.status) : subject.status != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (about != null ? about.hashCode() : 0);
        result = 31 * result + (teachers != null ? teachers.hashCode() : 0);
        result = 31 * result + (group != null ? group.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Subject{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", about='" + about + '\'' +
                ", teachers=" + teachers + '\'' +
                ", group=" + group +
                ", status=" + status +
                '}';
    }
}
